<!DOCTYPE html>
<html>
<head>
  <base href="/">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>Toyqo儿童玩具商城系统后台</title>
  <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo">后台</div>
    <!-- 头部区域（可配合layui已有的水平导航） -->

    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item">
        <a href="javascript:;">
          <img id="adminimg" src="front/picture/morentouxiang.png" class="layui-nav-img" onclick="openFileChooser()">
          <input type="hidden" id="addimgurl" value="headimgurl">
          <!-- 新增一个文件输入框，用于选择文件 -->
          <input type="file" id="fileInput" style="display: none;">
          <span id="adminname">贤心</span>
        </a>
        <dl class="layui-nav-child">
          <dd><a href="" id="editpassword">修改密码</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item"><a href="" id="logout">退出</a></li>
    </ul>
  </div>

  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-nav-tree"  lay-filter="test">
        <li class="layui-nav-item layui-nav-itemed">
          <a class="" href="javascript:;">管理</a>
          <dl class="layui-nav-child">
            <dd><a href="javascript:;" _url="sys/user">用户管理</a></dd>
            <dd><a href="javascript:;" _url="sys/product">商品管理</a></dd>
            <dd><a href="javascript:;" _url="sys/category">商品分类管理</a></dd>
            <dd><a href="javascript:;" _url="sys/orders">订单管理</a></dd>
            <dd><a href="javascript:;" _url="sys/admin">管理员管理</a></dd>
          </dl>
        </li>
      </ul>
    </div>
  </div>

  <div class="layui-body">
    <!-- 内容主体区域 -->
    <div style="padding: 15px;height:100%;">
      <iframe src="sys/example" id="main" style="height: 100%;width: 100%;border: none;"></iframe>
    </div>
  </div>

  <div class="layui-footer">
    <!-- 底部固定区域 -->
    © 2025.Company name All.
  </div>
</div>
<!--修改密码表单-->
<div style="display:none" id="editWin" lay-filter="editWin" class="layui-form">
  <div class="layui-form-item">
    <label class="layui-form-label">原密码</label>
    <div class="layui-input-inline">
      <input type="password" id="oldpwd" name="oldpwd" required lay-verify="required" placeholder="请输入原密码" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">新密码</label>
    <div class="layui-input-inline">
      <input type="password" id="newpwd" name="newpwd" required lay-verify="required" placeholder="请输入原密码" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">确认密码</label>
    <div class="layui-input-inline">
      <input type="password" id="confirm" name="confirm" required lay-verify="required" placeholder="请确认密码" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <button type="button" id="pwdsave" lay-submit lay-filter="pwdsave" class="layui-btn">保存</button>
  </div>
</div>
<script src="layui/layui.all.js"></script>
<script>
  //点击菜单在中心区域显示对应的页面
  const $=layui.$;
  $("[_url]").click(function(){
    let url=$(this).attr("_url");
    $("#main").attr("src",url);
  });
  //显示用户名
  $.ajax({
    url:'administrator/get',
    success:function(result){
      $('#adminname').text(result.name);
      $('#adminimg').attr("src",result.imgurl);
    },
    error:function(){
      $('#adminname').text('admin');
    }
  });
  //退出登录
  $('#logout').click(function(e) {
    e.preventDefault();
    $.ajax({
      url: 'administrator/logout',
      success: function() {
        window.location.href = 'sys/login';
      },
      error: function(xhr) {
        console.error('退出失败:', xhr.statusText);
        window.location.href = 'sys/login';
      }
    });
  });
  //修改密码
  $('#editpassword').click(function(e){
    e.preventDefault();
    layer.open({
      type:1,
      title:'修改密码',
      area:['400px','400px'],
      content:$("#editWin")
    });
  });
  //修改密码
  $('#pwdsave').on('click', function () {
    const currentPwd = $('#oldpwd').val().trim();
    const newPwd = $('#newpwd').val().trim();
    const confirmPwd = $('#confirm').val().trim();

    // 当前密码、新密码、确认新密码非空
    if (!currentPwd || !newPwd || !confirmPwd) {
      layer.msg('当前密码、新密码和确认新密码都不能为空');
      return;
    }
    if (newPwd!==confirmPwd) {
      layer.msg('新密码和确认新密码不一致');
      return;
    }
    // 验证当前密码是否正确
    $.ajax({
      url: 'administrator/verifyPassword',
      data:{password:currentPwd},
      success: function (result) {
        if(result){
          $.ajax({
            url: 'administrator/edit',
            data:{password:newPwd},
            success: function () {
              layer.msg('信息更新成功');
              layer.closeAll();
            },
            error: function () {
              layer.msg('信息更新失败');
            }
          });
        }else{
          layer.msg('当前密码不正确');
        }
      }
    });
  });

  //上传图片
  function openFileChooser() {
    document.getElementById('fileInput').click();
  }
  document.getElementById('fileInput').addEventListener('change', function() {
    console.log($('#addimgurl').val());
    const file = this.files[0];
    if (file) {
      const formData = new FormData();
      formData.append('file', file);

      // 发送 AJAX 请求上传图片
      fetch('/file/upload', {
        method: 'POST',
        body: formData
      })
              .then(response => response.json())
              .then(data => {
                // 根据后端数据结构，正确解析图片 URL
                if (data.data && data.data.length > 0) {
                  const imgUrl = data.data[0].src;
                  document.getElementById('adminimg').src = imgUrl;
                  document.getElementById('addimgurl').value = imgUrl;
                  $.ajax({
                    url:'administrator/edit',
                    data:{imgurl:imgUrl},
                    success:function(){
                      layer.msg("头像上传成功！");
                    },
                    fail:function(){
                      layer.msg("头像上传失败！");
                    }
                  });
                } else {
                  console.error('返回的数据格式无效:', data);
                }
              })
              .catch(error => {
                console.error('上传图片时出错:', error);
              });
    }
    console.log($('#addimgurl').val());
  });

</script>
</body>
</html>